package com.neouton.crm.managerPayRequest.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neouton.common.util.AuthUtils;
import com.neouton.common.util.PageUtil;
import com.neouton.config.ResponseAPI;
import com.neouton.crm.entity.SysUserEntity;
import com.neouton.crm.managerPayRequest.entity.ManagerPayRequest;
import com.neouton.crm.managerPayRequest.entity.ManagerPaymentTable;
import com.neouton.crm.managerPayRequest.service.ManagerPayRequestService;
import com.neouton.crm.pm.service.ProjectMemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/managerPayRequest")
public class ManagerPayRequestController {

    @Resource
    private ManagerPayRequestService managerPayRequestService;
    @Resource
    private ProjectMemberService projectMemberService;

    /**
     * 列表信息查询
     *
     */
    @PostMapping("/queryManagerPayRequestList/{pageNum}/{pageSize}")
    public ResponseAPI queryManagerPayRequestList(@PathVariable(value = "pageNum") Integer pageNum,
                                        @PathVariable(value = "pageSize") Integer pageSize,
                                        @RequestBody ManagerPayRequest managerPayRequest,
                                        @RequestHeader("token") String token) {
        List<SysUserEntity> userList = projectMemberService.queryUserList(AuthUtils.getUserId(),"managerPayRequest", token);
        IPage<ManagerPayRequest> page = new Page<>(pageNum, pageSize, true);
        PageUtil<ManagerPayRequest> list = managerPayRequestService.queryManagerPayRequestList(page, managerPayRequest, userList, token);
        return ResponseAPI.successData(list);
    }

    /**
     * 列表信息查询
     *
     */
    @PostMapping("/queryManagerPayRequestApprovalList/{pageNum}/{pageSize}")
    public ResponseAPI queryManagerPayRequestApprovalList(@PathVariable(value = "pageNum") Integer pageNum,
                                                  @PathVariable(value = "pageSize") Integer pageSize,
                                                  @RequestBody ManagerPayRequest managerPayRequest,
                                                  @RequestHeader("token") String token) {
        IPage<ManagerPayRequest> page = new Page<>(pageNum, pageSize, true);
        PageUtil<ManagerPayRequest> list = managerPayRequestService.queryManagerPayRequestApprovalList(page, managerPayRequest, token);
        return ResponseAPI.successData(list);
    }


    @PostMapping("/saveManagerPayRequest")
    public ResponseAPI saveManagerPayRequest(@RequestBody ManagerPayRequest managerPayRequest,@RequestHeader("token") String token) {
        return ResponseAPI.successData(managerPayRequestService.saveManagerPayRequest(managerPayRequest,token));
    }

    @PostMapping("/queryManagerPayRequestMoney")
    public ResponseAPI queryManagerPayRequestMoney(@RequestBody ManagerPayRequest managerPayRequest) {
        return ResponseAPI.successData(managerPayRequestService.queryManagerPayRequestMoney(managerPayRequest));
    }

    @PostMapping("/approvedManagerPayRequest/")
    public ResponseAPI approvedManagerPayRequest(@RequestBody ManagerPayRequest entity, @RequestHeader("token") String token) {
        return ResponseAPI.successData(managerPayRequestService.approvedManagerPayRequest(entity,token,false));
    }

    @PostMapping("/rejectionManagerPayRequest")
    public ResponseAPI rejectionManagerPayRequest(@RequestBody ManagerPayRequest entity, @RequestHeader("token") String token) {
        return ResponseAPI.successData(managerPayRequestService.rejectionManagerPayRequest(entity,token));
    }

    @PostMapping("/queryManagerPayRequestHistory/{approvalId}")
    public ResponseAPI queryManagerPayRequestHistory(@PathVariable(value = "approvalId") Integer approvalId) {
        return ResponseAPI.successData(managerPayRequestService.queryManagerPayRequestHistory(approvalId));
    }

    @PostMapping("/updatePayedRecordTableData/{approvalId}")
    public ResponseAPI updatePayedRecordTableData(@PathVariable(value = "approvalId") Integer approvalId) {
        managerPayRequestService.updatePayedRecordTableData(approvalId);
        return ResponseAPI.success();
    }

    @PostMapping("/queryPayTablesByIdAndPlanPeriod")
    @ResponseBody
    public ResponseAPI queryPayTablesByIdAndPlanPeriod(@RequestBody ManagerPaymentTable entity) {
        List<ManagerPaymentTable> list = managerPayRequestService.queryPayTablesByIdAndPlanPeriod(entity);
        return ResponseAPI.successData(list);
    }
    @PostMapping("/deletePayedRequestById/{payRequestId}")
    public ResponseAPI deletePayedRequestById(@PathVariable(value = "payRequestId") Integer payRequestId) {
        Boolean flag= managerPayRequestService.deletePayedRequestById(payRequestId);
        return ResponseAPI.successData(flag);
    }
    /**
     * 导出Excel
     * @param response
     * @param entity
     * @throws Exception
     */
    @PostMapping("/exportInfo")
    public void exportInfo(HttpServletResponse response, @RequestBody ManagerPayRequest entity, @RequestHeader("token") String token) throws Exception{
        List<SysUserEntity> userList = projectMemberService.queryUserList(AuthUtils.getUserId(),"managerPayRequest", token);
        List<ManagerPayRequest> list = managerPayRequestService.queryPaymentRequestListInfo(entity, userList, token);
        managerPayRequestService.exportPaymentRequestInfo(response,list,token);
    }

}
